package com.ruoyi.biz.service.impl;

import com.ruoyi.biz.domain.BizOrder;
import com.ruoyi.biz.domain.BizParcel;
import com.ruoyi.biz.domain.BizParcelPic;
import com.ruoyi.biz.domain.vo.BizOrderParcelVO;
import com.ruoyi.biz.domain.vo.BizOrderVO;
import com.ruoyi.biz.mapper.BizOrderMapper;
import com.ruoyi.biz.mapper.BizParcelMapper;
import com.ruoyi.biz.mapper.BizParcelPicMapper;
import com.ruoyi.biz.mapper.BizStatePriceConfigMapper;
import com.ruoyi.biz.service.IBizOrderService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.pay.PoliPayService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-18
 */
@Service
public class BizOrderServiceImpl implements IBizOrderService {
    @Autowired
    private BizOrderMapper bizOrderMapper;
    @Autowired
    private BizParcelMapper bizParcelMapper;
    @Autowired
    private BizParcelPicMapper bizParcelPicMapper;
    @Autowired
    private BizStatePriceConfigMapper bizStatePriceConfigMapper;
    @Autowired
    private PoliPayService poliPayService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public BizOrder selectBizOrderById(Long id) {
        return bizOrderMapper.selectBizOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param bizOrder 订单
     * @return 订单
     */
    @Override
    public List<BizOrder> selectBizOrderList(BizOrder bizOrder) {
        return bizOrderMapper.selectBizOrderList(bizOrder);
    }

    /**
     * 查询订单详细列表
     *
     * @param bizOrder 订单
     * @return 订单
     */
    @Override
    public List<BizOrderVO> selectBizOrderVOList(BizOrder bizOrder) {
        List<BizOrderVO> result = new ArrayList<>();
        List<BizOrder> orders = bizOrderMapper.selectBizOrderList(bizOrder);
        for (BizOrder order : orders) {
            result.add(buildOrderVO(order));
        }
        return result;
    }


    /**
     * 通过订单ID查询订单详情
     *
     * @param orderId 订单ID
     * @return
     */
    @Override
    public BizOrderVO selectBizOrderVOById(long orderId) {
        BizOrder bizOrder = bizOrderMapper.selectBizOrderById(orderId);
        return buildOrderVO(bizOrder);
    }

    /**
     * 计算订单价格
     *
     * @param parcelIdsStr
     * @return
     */
    @Override
    public BigDecimal computePrice(String parcelIdsStr) {
        List<Long> parcelIds = Arrays.asList(parcelIdsStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        List<BizParcel> pracels = bizParcelMapper.selectBizParcelByIds(parcelIds);


        return null;
    }


    private BizOrderVO buildOrderVO(BizOrder bizOrder) {
        BizOrderVO result = new BizOrderVO();
        BeanUtils.copyProperties(bizOrder, result);
        List<Long> orderIds = new ArrayList<>();
        orderIds.add(bizOrder.getId());
        List<BizParcel> bizParcels = bizParcelMapper.selectBizParcelByOrderIds(orderIds);
        List<BizOrderParcelVO> bizOrderParcelVOs = new ArrayList<>();
        for (BizParcel data : bizParcels) {
            BizOrderParcelVO parcelVo = new BizOrderParcelVO();
            parcelVo.setPracelId(data.getId());
            parcelVo.setParcelNo(data.getParcelNo());
            parcelVo.setItemDesc(data.getItemDesc());
            BizParcelPic condition = new BizParcelPic();
            condition.setParcelId(data.getId());
            List<BizParcelPic> pics = bizParcelPicMapper.selectBizParcelPicList(condition);
            parcelVo.setPics(pics);
            bizOrderParcelVOs.add(parcelVo);
        }
        result.setParcelVOs(bizOrderParcelVOs);
        return result;
    }

    @Override
    public BizOrder createBizOrder(BizOrder bizOrder, String parcelIds) {
        //生成订单号
        bizOrder.setOrderNo(generateOrderNo());
        bizOrder.setCreateTime(DateUtils.getNowDate());
        bizOrder.setUpdateTime(DateUtils.getNowDate());
        bizOrderMapper.insertBizOrder(bizOrder);
        //回填邮包表订单ID
        bizParcelMapper.updateOrderId(Arrays.asList(parcelIds.split(",")), bizOrder.getId());
        //生成支付链接
        return bizOrder;
    }

    @Override
    public String startPay(BizOrder bizOrder) {
        bizOrder.setUpdateTime(DateUtils.getNowDate());
        bizOrderMapper.updateBizOrder(bizOrder);
        //生成支付链接
        return poliPayService.startPay(bizOrder.getOrderAmount(), bizOrder.getOrderNo());
    }

    @Override
    public String getTransaction(String token) {
        //生成支付链接
        return poliPayService.getTransaction(token);
    }

    //生成订单号 (暂时时间戳加6位随机数)
    private String generateOrderNo() {
        StringBuilder sb = new StringBuilder();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        sb.append(time);
        sb.append((int) ((Math.random() * 9 + 1) * 100000));
        return sb.toString();
    }

    /**
     * 新增订单
     *
     * @param bizOrder 订单
     * @return 结果
     */
    @Override
    public int insertBizOrder(BizOrder bizOrder) {
        bizOrder.setCreateTime(DateUtils.getNowDate());
        return bizOrderMapper.insertBizOrder(bizOrder);
    }

    /**
     * 修改订单
     *
     * @param bizOrder 订单
     * @return 结果
     */
    @Override
    public int updateBizOrder(BizOrder bizOrder) {
        bizOrder.setUpdateTime(DateUtils.getNowDate());
        return bizOrderMapper.updateBizOrder(bizOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteBizOrderByIds(Long[] ids) {
        return bizOrderMapper.deleteBizOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteBizOrderById(Long id) {
        return bizOrderMapper.deleteBizOrderById(id);
    }


    private void binning() {

    }
}
